package ru.bragnikita.utils.swing.widget;

import ru.bragnikita.utils.swing.widget.field.*;

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author Nikita.Brazhnikov
 */
public class FormPanel {

    private JPanel panel;

    private int col = 0;
    private int row = 0;

    private Map<String, WtTextWidget> widgetMap = new TreeMap<>();

    public FormPanel() {
        this(null);
    }

    public FormPanel(String name) {
        panel = new JPanel(new GridBagLayout());
        if (name != null) {
            panel.setBorder(BorderFactory.createTitledBorder(name));
        }
    }

    public FormPanel add(Field field) {
        WtTextField widget = new WtTextField(field);
        JLabel label = widget.getLabel();
        JComponent comp = widget.getComponent();
        comp.setMinimumSize(comp.getPreferredSize());
        placeTextField(label, comp);
        widgetMap.put(widget.getId(), widget);
        return this;
    }

    public FormPanel newCol() {
        col++;
        row = 0;
        return this;
    }

    private void placeTextField(JLabel label, JComponent component) {
        final GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(3, 5, 3, 5);
        c.anchor = GridBagConstraints.LINE_END;
        c.gridy = row;
        c.gridx = col * 2;
        panel.add(label, c);
        c.gridx++;
        c.anchor = GridBagConstraints.LINE_START;
        panel.add(component, c);
        row++;
    }

    public JPanel getPanel() {
        return panel;
    }



    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

//                JPanel jp = new JPanel(new GridBagLayout());
//                GridBagConstraints c = new GridBagConstraints();
//                c.insets = new Insets(5, 5, 5, 5);
//                c.gridx = 0;
//                c.gridy = 0;
//                c.anchor = GridBagConstraints.LINE_END;
//                jp.add(new JLabel("111111"), c);
//                c.gridx = 1;
//                c.anchor = GridBagConstraints.LINE_START;
//                jp.add(new JTextField(12), c);
//                c.gridx = 0;
//                c.gridy = 1;
//                c.anchor = GridBagConstraints.LINE_END;
//                jp.add(new JLabel("13"), c);
//                c.gridx = 1;
//                c.anchor = GridBagConstraints.LINE_START;
//                jp.add(new JTextField(16), c);

                FormPanel formPanel = new FormPanel("Database");
                formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("user", "User").length(10));
                formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("password", "Password").length(15));
                formPanel.newCol();
                formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("url", "URL").length(25));
                formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("sid", "SID").length(5));
                formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("port", "Port").length(4));
                JPanel jp = formPanel.getPanel();

                frame.add(jp, BorderLayout.CENTER);
                frame.setSize(300, 400);
                frame.setVisible(true);

            }
        });
    }

}
